﻿using System;
using System.Web.UI.WebControls;
using BLL;
using Model;
using ZedGraph;
using System.Collections.Generic;
using System.Drawing;

public partial class StatisticData_PackageTimeStatistic_PackageStatisticbyPID_D : System.Web.UI.Page
{
    private string var_productTypeID
    {
        get { return ViewState["var_productTypeID"].ToString(); }
        set { ViewState["var_productTypeID"] = value; }
    }
    private DateTime var_starttime
    {
        get { return DateTime.Parse(ViewState["starttime"].ToString()); }
        set { ViewState["starttime"] = value; }
    }
    private DateTime var_stoptime
    {
        get { return DateTime.Parse(ViewState["stoptime"].ToString()); }
        set { ViewState["stoptime"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCustoms();
        }
        InitializeComponent();
    }

    private void InitializeComponent()
    {
        this.zedgraphweb1.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(this.zedgraphweb1_rendergraph);//注册事件
    }

    protected void BindCustoms()
    {
        Customer cust = new Customer();
        this.DropDownList1.DataSource = cust.LoadEntities();
        this.DropDownList1.DataTextField = "CustomerName";
        this.DropDownList1.DataValueField = "CustomerID";
        this.DropDownList1.DataBind();
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        BindPackages();
        InitializeComponent();
        this.zedgraphweb1.Visible = true;
    }

    private void BindPackages()
    {
        var_starttime = DateTime.Parse(this.TextBox1.Text + " 00:00:00");
        var_stoptime = DateTime.Parse(this.TextBox2.Text + " 00:00:00");
        var_productTypeID = this.DropDownList1.SelectedItem.Text.Trim();

        PackageProduct_QT test = new PackageProduct_QT();
        GridView1.DataSource = test.GetProductType_QT(var_productTypeID, var_starttime, var_stoptime);
        GridView1.DataBind();        
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindPackages();
    }

    protected void zedgraphweb1_rendergraph(ZedGraph.Web.ZedGraphWeb webObject, System.Drawing.Graphics g, MasterPane pane)
    {
        int i = 0;
        int j = 0;
        PackageProduct_QT t_qt = new PackageProduct_QT();
        List<Time_QInfo> t_qs = (List<Time_QInfo>)t_qt.GetProductType_QT(var_productTypeID, var_starttime, var_stoptime);

        GraphPane myPane = pane[0];

        myPane.Title.Text = "按日历日统计包装量";
        myPane.XAxis.Title.Text = "日历日";
        myPane.YAxis.Title.Text = "包装量";

        //创建曲线
        PointPairList list = new PointPairList();
        foreach (Time_QInfo t_q in t_qs)
        {
            double y = (double)t_q.Quantity;
            int x = i;
            i++;
            list.Add(x, y);
        }
        LineItem myCurve = myPane.AddCurve("", list, Color.Green, SymbolType.Square);
        //myCurve.Line.IsSmooth = true;
        //myPane.AddBar("", list, Color.Green);

        string[] labels = new string[t_qs.Count];
        //给x轴上的每一个间隔点加上说明文字
        foreach (Time_QInfo t_q in t_qs)
        {
            labels[j] = t_q.DayTime;
            j++;
        }

        myPane.XAxis.Scale.TextLabels = labels; //这里设置的是TextLabels属性，所以下面x轴的类型必须也设置为Text   

        //设置x轴的样式
        myPane.XAxis.Type = AxisType.Text;
        MyGraphStyle.BuildbarGraphStyle(myPane);

        pane.AxisChange(g);
    }
}
